//
//  AppropriateDetailController.m
//  Food
//
//  Created by dlios on 15-7-16.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "AppropriateDetailController.h"
#import "AFNet.h"
#import "MenuSearchPage_Class.h"
#import "Reachability.h"
#import "MBProgressHUD.h"

@interface AppropriateDetailController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *suitableArr;
@property (nonatomic, retain) NSMutableArray *restrainArr;
@property (nonatomic, retain) NSMutableArray *sourceArr;
@property (nonatomic, retain) NSMutableDictionary *dataDic;
@property (nonatomic, retain) MBProgressHUD *HUD;
@end

@implementation AppropriateDetailController

- (void)dealloc
{
    [_HUD release];
    [_dataDic release];
    [_sourceArr release];
    [_restrainArr release];
    [_tableView release];
    [_suitableArr release];
    [_menuClass release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //数组初始化
    self.suitableArr = [NSMutableArray array];
    self.restrainArr = [NSMutableArray array];
    self.sourceArr = [NSMutableArray array];
    
    
    [self dataHander];
}

//创建视图
- (void)setUpSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.allowsSelection = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    [_tableView release];
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    //菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.removeFromSuperViewOnHide = YES;
    self.HUD.labelText = @"稍等";
    self.HUD.detailsLabelText = @"正在加载中";
    self.HUD.square = YES;
    [self.view addSubview:self.HUD];
    self.HUD.mode = MBProgressHUDModeIndeterminate;

    
}
//协议必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.suitableArr.count;
    }else {
        return self.restrainArr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    //cell赋值
   if (indexPath.section == 0) {
        NSString *str = [self.dataDic objectForKey:@"name"];
        NSString *str1 = [[self.suitableArr objectAtIndex:indexPath.row] objectForKey:@"name"];
        NSString *str2 = [[self.suitableArr objectAtIndex:indexPath.row] objectForKey:@"content"];
        NSString *str3 = [NSString stringWithFormat:@"%@ + %@ : %@", str, str1, str2];
        cell.textLabel.text = str3;
        cell.textLabel.numberOfLines = 0;
        [cell.textLabel sizeToFit];
    }else {
        NSString *stu = [self.dataDic objectForKey:@"name"];
        NSString *stu1 = [[self.restrainArr objectAtIndex:indexPath.row] objectForKey:@"name"];
        NSString *stu2 = [[self.restrainArr objectAtIndex:indexPath.row] objectForKey:@"content"];
        NSString *stu3 = [NSString stringWithFormat:@"%@ + %@ : %@", stu, stu1, stu2];
        cell.textLabel.text = stu3;
        cell.textLabel.numberOfLines = 0;
        [cell.textLabel sizeToFit];
        
    }
    
    
    return cell;
}
// 设置分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.restrainArr.count != 0 && self.suitableArr.count != 0) {
        return 2;
    }else {
        return 1;

    }
}
// 设置分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
// 自定义header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor lightGrayColor];
        label.text = @"相克";
        label.font = [UIFont systemFontOfSize:20];
        label.textAlignment = NSTextAlignmentCenter;
        return [label autorelease];
    }
    else if (section == 1) {
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor lightGrayColor];
        label.text = @"相宜";
        label.font = [UIFont systemFontOfSize:20];
        label.textAlignment = NSTextAlignmentCenter;
        return [label autorelease];
    }
    return nil;
}

// 数据处理
- (void)dataHander
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败! 请检查网络吧" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
        [self.HUD hide:YES];
    }

    [AFNet connectNetGetDataWithURLString:[NSString stringWithFormat:@"http://api.xiangha.com/shicai3/getIngreInfo?code=%@&type=taboo", self.menuClass.codeId] parameters:nil finishblock:^(id result) {
        [self.HUD show:YES];
        
        self.dataDic = [result objectForKey:@"data"];
        self.sourceArr = [[result objectForKey:@"data"]objectForKey:@"taboo"];
        
        for (NSDictionary *dic in self.sourceArr) {
            if ([[dic valueForKey:@"state"] integerValue] == 1) {
                [self.suitableArr addObject:dic];
            } else {
                [self.restrainArr addObject:dic];
            }
        }
        [self setUpSubView];
        [self.HUD hide:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
